<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description: 获取地址列表
 *
 * @author      Eden
 * @datetime    2016/12/2 19:34
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */


class Lists_Model extends API_Model
{

    // 参数
    private $params = array();

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run(Array $params)
    {
        $send['state'] = false;
        do {

            if (empty($params['userId'])) {
                $send['error'] = '参数错误';
                break;
            }

            $query = $this->db->get_where('user_address', array(
                'userId' => $params['userId'],
                'status' => 1
            ));
            $rows = $query->result_array();

            if (empty($rows)) {
                $send['state'] = true;
                $send['total'] = 0;
                $send['data'] = array();
                break;
            }

            $rows = $this->afterSelect($rows);
            $send['state'] = true;
            $send['total'] = count($rows);
            $send['data'] = $rows;

        } while(false);
        $this->output->send($send);
    }

    /**
     * 地址列表
     * @param $rows
     * @return array
     */
    public function afterSelect($rows)
    {
        $array = array();
        if (!empty($rows) && is_array($rows)) foreach ($rows as $row) {
            $array[] = array(
                'addressId' => (int)$row['id'],
                'name' => value($row, 'name'),
                'phone' => value($row, 'phone'),
                'province' => value($row, 'province'),
                'city' => value($row, 'city'),
                'area' => value($row, 'area'),
                'address' => value($row, 'address'),
                'created' => value($row, 'created'),
                'default' => !empty($row['default'])
            );
        }
        return $array;
    }

}

